package com.dp.service.jdc;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.pojo.ChannelVo;
import com.dp.pojo.News;
import com.dp.service.impl3.BaseImpl;
import com.dp.vo.NewsContenttitle;
import com.dp.vo.TChannels;

@Service
@Transactional
public class IndexServiceImpl extends BaseImpl {

    
    /**获取二级导航
     * @param navChannels
     * @return
     */
    @Deprecated
    @SuppressWarnings("unchecked")
    public List<TChannels> getNav2Channels(){
        List<TChannels> nav2ChannelsList = null;
        String hql = "select tc from TChannels tc where tc.level=4 order by tc.channelId";
        try {
            nav2ChannelsList=this.getQueryList(hql);
            return nav2ChannelsList;
            
        } catch (Exception e) {
            logger.error(" IndexServiceImpl-->>getNav2Channels fail !!!" +e.getMessage());
            e.printStackTrace();
            return null;
        }
        
        
    }
   
    
    public String getfullpath(int rid){
        String fullPath="";
               sql=" select fullpath from news_resources  b where b.rid="+rid+""; 
//                  List<Object[]> s=emr.createNativeQuery(sql).getResultList();
               try{
                   
                   fullPath= this.getOneString();
                   return fullPath;
               }catch (Exception e) {
                   logger.error(" IndexServiceImpl-->>getNavChannels fail !!!" +e.getMessage());
                   e.printStackTrace();
                   return fullPath;
            }
   }
    /**
     * 根据upId获取他下一级栏目列表
     * @return
     */
    @Deprecated
    @SuppressWarnings("unchecked")
    public List<TChannels> getNavChannels(String upId){
        List<TChannels> navChannelLists = null; 
        String hql = "select tc from TChannels tc where tc.previousId='"+upId+"' order by tc.channelId ";
        try {
            navChannelLists=this.getQueryList(hql);
            return navChannelLists;
            
        } catch (Exception e) {
            logger.error(" IndexServiceImpl-->>getNavChannels fail !!!" +e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
    
    
	/**获取当前栏目下一级的栏目列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TChannels> getChannelsList(String upId) {
		List<TChannels> channelList = null; 
		
		this.sql = "SELECT t FROM TChannels t where t.previousId='"+upId+"'";
		try {
			channelList=this.getQueryList(sql);
			return channelList;
			
		} catch (Exception e) {
			logger.error("IndexServiceImpl-->> getChannelsList fail !!!" +e.getMessage());
			e.printStackTrace();
			return null;
		}
	}
	/**根据channelId获取channel下的前5个新闻标题,创建时间等
	 * @return
	 */
	public  List<News> getNewTitalList(String channelId,int limit){
		List<News> 	newsLsit = new ArrayList<News>();
		
		News news = null;
		try{
			this.sql = "select  nc.contenttitle,nc.cid,tc.channel_id,nc.updatetime ,nc.contentid,nc.contenticon " +
					"from news_contenttitle nc ," +
			"news_extends_channel_title nect,news_extends_channel nec,t_channels tc " +
			"where nc.cid=nect.cid and nect.channleid=nec.channleid and tc.channel_id=nec.channel_id and " +
			"nect.contentstatus = 1 and tc.channel_id='"+channelId+"' order by nect.titleorder,nc.cid desc limit "+limit;
			
			List<Object[]> temp=this.em.createNativeQuery(sql).getResultList();
			for(Object[]o:temp)
			{
				news = new News();
				news.setContentName(this.getString(o[0]));
				news.setCId(this.getString(o[1]));
				news.setChannelId(this.getString(o[2]));
				news.setCreatedDate(this.getString(o[3]).split(" ")[0].replace("//", "-"));
				news.setContentId(this.getString(o[4]));
				news.setContentIcon(this.getString(o[5]));
				newsLsit.add(news);
			}
		}catch (Exception e) {
			logger.error("IndexServiceImpl-->>getNewTitalList fail !!!" + e.getMessage());
			e.printStackTrace();
		}
		return newsLsit;
		
	}
	/**根据图片channelId获取channel下的前5个图片标题,时间等
     * @return
     */
    public  List<News> getImgList(List<News> newsList){
        for (News news : newsList) {
            news.setResourceRid(this.getImgRid(news.getCId()));
        }
        return newsList;
    }
    /**根据图片新闻Cid获取图片的Rid
     * @param newsCId
     * @return
     */
    public int getImgRid(String newsCId){
        //可以在这设置默认的Rid
        int rid =0;
        this.sql="select nc.rid from news_contenresource nc " +
                "where  nc.cid="+newsCId;
        try{
            
            List<Object> temp=this.em.createNativeQuery(sql).getResultList();
            rid = Integer.parseInt(this.getString(temp.get(0)));
        }catch (Exception e) {
            logger.error("IndexServiceImpl-->>getImgRid fail !!!" + e.getMessage());
            e.printStackTrace();
        }
        return rid;
        
    }
    /**根据图片新闻的Cid获取图片新闻的多个图片地址
     * @param newsCId
     * @return
     */
    public List<String> getImg(String newsCId){
        List<String> imgList= new ArrayList<String>();
        this.sql="select nr.fullpath from news_resources nr,news_contenresource nc " +
        		"where nr.rid=nc.rid and nc.cid="+newsCId;
        try{
            
            List<Object> temp=this.em.createNativeQuery(sql).getResultList();
            for(Object o:temp){
                imgList.add(this.getString(o));
            }
        }catch (Exception e) {
            logger.error("IndexServiceImpl-->>getImg fail !!!" + e.getMessage());
            e.printStackTrace();
        }
        return imgList;
        
    }
    /**根据图片文件路径,更改成-p_240x320.png路径
     * @return
     */
    public String convertFullPath(String fullPath){
        if(fullPath!=null&&fullPath.length()!=0){
            int length = fullPath.length()-4;
            String[] fileEnd =  {"_320x480.png","_800x400.png","_800x400.jpg","_480x800.jpg","_320x480.jpg","_240x320.jpg","_174x192.jpg"};
            File file;;
            int i=0;
            do{
                fullPath = fullPath.substring(0,length);
                fullPath+=fileEnd[i].toString();
                file=new File(fullPath);
                i++;
            }while((!file.exists()&&i<fileEnd.length));
            if(!file.exists()){
                fullPath="opt\\242_upload\\2013\\05\\09\\1\\hnl2hghy7xdz.jpg";
            }
        }else {
            fullPath="opt\\242_upload\\2013\\05\\09\\1\\hnl2hghy7xdz.jpg";
        }
        return fullPath;
    }
	
	
}